#!/usr/bin/env node

// CLI文件头必须是这个

/**
 * 1.通过命令交互询问用户问题
 * 2.根据问题的答案来生成文件
 */

const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const ejs = require('ejs');

inquirer.prompt([
    {
        type: 'input',
        name: 'name',
        message: 'project name?'
    }
])
.then(answer => {
    console.log(answer)

    // 模板目录
    const tplDir = path.join(__dirname, 'templates');
    // 目标目录
    const destDir = process.cwd();

    fs.readdir(tplDir, (err, files) => {
        if (err) throw err;
        console.log(files);
        files.forEach(file => {
            ejs.renderFile(path.join(tplDir, file), answer, (err, result) => {
                if (err) throw err;
                console.log(result);
                fs.writeFileSync(path.join(destDir, file), result)
            })
        })
    })
})